package com.jyx.search.controller;

import com.jyx.api.learning.CourseLearningControllerApi;
import com.jyx.api.learning.CourseQueryLearningApi;
import com.jyx.framework.domain.learning.responses.GetMediaResult;
import com.jyx.framework.domain.learning.responses.LearningStatusResult;
import com.jyx.framework.exception.ExceptionCast;
import com.jyx.framework.model.response.CommonCode;
import com.jyx.framework.utils.JyxOauth2Util;
import com.jyx.search.service.CourseQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
@RestController
@RequestMapping("/learning")
public class CourseQueryLearning implements CourseQueryLearningApi {

    @Autowired
    CourseQueryService courseQueryService;

    @Override
    @PostMapping("/choosecourse/learnstatus/{id}")
    public LearningStatusResult getLearningStatus(@PathVariable("id") String courseId, HttpServletRequest request) {
        JyxOauth2Util jyxOauth2Util = new JyxOauth2Util();
        JyxOauth2Util.UserJwt userJwt = jyxOauth2Util.getUserJwtFromHeader(request);
        if(userJwt == null){
            ExceptionCast.cast(CommonCode.UNAUTHENTICATED);
        }
        String userId = userJwt.getId();
        return courseQueryService.getLearningStatusByIds(courseId,userId);
    }

}
